package services

import (
    "context"
    "gitee.com/go-libs/db-xorm/db"
    "integration/app/models"
)

func (o *ErpGoodsService) GetByErpCode(ctx context.Context, code string) (model *models.ErpGoods, err error) {
    var (
        has  bool
        sess *db.Session
    )

    if sess, err = o.Slave(ctx); err != nil {
        return
    }

    model = &models.ErpGoods{}

    if has, err = sess.Where("`erp_code` = ?", code).Get(model); err != nil || !has {
        model = nil
    }
    return
}

func (o *ErpGoodsService) GetByErpId(ctx context.Context, val int) (model *models.ErpGoods, err error) {
    var (
        has  bool
        sess *db.Session
    )

    if sess, err = o.Slave(ctx); err != nil {
        return
    }

    model = &models.ErpGoods{}

    if has, err = sess.Where("`erp_id` = ?", val).Get(model); err != nil || !has {
        model = nil
    }
    return
}

func (o *ErpGoodsService) Edit(ctx context.Context, req *models.ErpGoods) (affects int64, err error) {
    var sess *db.Session

    if sess, err = o.Master(ctx); err != nil {
        return
    }

    // Send data
    // to master session and return affect count.
    affects, err = sess.Cols("erp_code", "erp_name", "erp_tax", "erp_warehouse").Where("`id` = ?", req.Id).Update(req)
    return
}
